package com.fingard.dsp.bank.directbank.cmbunion02;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.directbank.cmbunion02.utils.SignatureUtil;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.StringHelper;
import org.apache.commons.lang.StringEscapeUtils;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class CMBUnion02OtherElse extends CMBUnion02Base implements IOtherElse {
    @Override
    public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
        RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
        try{
            String transType = p_reqElse.reqHeader.transType;
            if ("VerifySign".equals(transType)) {
                String ownAct = "";
                if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
                    ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
                }
                String plain = "";
                if (p_reqElse.reqHashMap.containsKey("Plain")) {
                    plain = URLDecoder.decode(p_reqElse.reqHashMap.get("Plain"), "utf-8");
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "缺失验签原始串");

                    return retAts;
                }
                WriteBankLogLn("招商聚合支付通知报文："+plain);
                ActSetItem actItem = this.getBankConfig().getActSetItem(ownAct, "");
                if (actItem != null) {
                    Map<String, String> params = convertToMap(plain);
                    String sign = params.remove("sign");
                    String contentStr = SignatureUtil.getSignContent(params);
                    boolean result = SignatureUtil.rsaCheck(contentStr, sign, actItem.oppCertFilePath, "UTF-8");
                    retAts.retHashMap = new HashMap<String, String>();
                    if (result) {
                        retAts.retHashMap.put("RespCode", "0");
                        retAts.retHashMap.put("RespInfo", "验签通过");
                    } else {
                        retAts.retHashMap.put("RespCode", "-1");
                        retAts.retHashMap.put("RespInfo", "验签失败");
                    }
                    WriteBankLogLn("招商聚合验签结果1："+retAts);
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
                }
            }else if ("UnifiedSign".equals(transType)) {
                ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqElse.reqHashMap.get("OurBankAccount"), "111");
                Map<String, String> requestPublicParams = getCommonMap();
                requestPublicParams.put("respCode",p_reqElse.reqHashMap.get("respCode"));
                requestPublicParams.put("returnCode",p_reqElse.reqHashMap.get("returnCode"));
                ObjectMapper mapper = new ObjectMapper();
                WriteBankLogLn("招商聚合加签前的报文内容：" + mapper.writeValueAsString(requestPublicParams));
                String signContent= SignatureUtil.getSignContent(requestPublicParams);
                String sign = SignatureUtil.rsaSign(signContent, actItem.ownKeyStorePath, "UTF-8");
                WriteBankLogLn2("签名结果:"+sign);
                retAts.retHashMap = new HashMap<String, String>();
                if (StringHelper.hasAnyChar(sign)) {
                    retAts.retHashMap.put("RespCode", "0");
                    retAts.retHashMap.put("RespInfo", "签名成功!");
                    retAts.retHashMap.put("Result", sign);
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "签名失败!");
                }
            }else{
                retAts.retHashMap = new HashMap<String, String>();
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);


            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.retHashMap.put("RespCode", "-1");
            retAts.retHashMap.put("RespInfo", "1111"+ex.getMessage());
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
